﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco;
using umbraco.BasePages;
using umbraco.BusinessLogic;
using umbraco.interfaces;
using umbraco.controls;
using Commerce.Common;
using System.Data;

namespace CommerceForUmbraco.adminSection.commerce
{
    public partial class dashboardCategoryTree : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.pane_cattree.Text = "Categories/Products";
            this.pp_cattreeoptions.Text = "Show Products";

            BindData();
        }

        protected void BindData()
        {
            try
            {
                StringBuilder sb = new StringBuilder();

                // get collection values
                Commerce.Common.CategoryCollection parentcategories = null;

                parentcategories = new CategoryCollection().Where("parentID", SubSonic.Comparison.Equals, 0).OrderByAsc("categoryName").Load();

                if (parentcategories.Count > 0)
                {
                    sb.Append("<ul>");
                    foreach (Category c in parentcategories)
                    {
                        sb.Append("<li style='list-style-type: none;'>");
                        sb.AppendFormat("<img src=\"/umbraco/images/umbraco/node-select.png\" /> <a href=\"javascript:openCategory({1})\">{0}</a>", c.CategoryName, c.CategoryID);

                        if (ShowProducts.Checked)
                        {
                            sb.Append(RenderProducts(c));
                        }
                        sb.Append(RenderSubcategories(c.CategoryID));

                        sb.Append("</li>");
                    }
                    sb.Append("</ul>");
                }

                treePH.Controls.Add(new LiteralControl(sb.ToString()));
            }
            catch (Exception e)
            {
                umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Error, umbraco.BusinessLogic.User.GetUser(0), -1, String.Format("{0} -- {1}", e.Message, e.StackTrace));
            }
        }

        protected string RenderSubcategories(int parentcatid)
        {
            StringBuilder sb = new StringBuilder();

            // get collection values
            Commerce.Common.CategoryCollection categories = null;

            categories = new CategoryCollection().Where("parentID", SubSonic.Comparison.Equals, parentcatid).OrderByAsc("categoryName").Load();

            if (categories.Count > 0)
            {
                sb.Append("<ul>");
                foreach (Category c in categories)
                {
                    sb.Append("<li style='list-style-type: none;'>");
                    sb.AppendFormat("<img src=\"/umbraco/images/umbraco/node-select-child.png\" /> <a href=\"javascript:openCategory({1})\">{0}</a>", c.CategoryName, c.CategoryID);

                    if (ShowProducts.Checked)
                    {
                        sb.Append(RenderProducts(c));
                    }
                    sb.Append(RenderSubcategories(c.CategoryID));

                    sb.Append("</li>");
                }
                sb.Append("</ul>");
            }

            return sb.ToString();
        }

        protected string RenderProducts(Category parentcat)
        {
            StringBuilder sb = new StringBuilder();

            // get collection values
            Commerce.Common.ProductCollection products = null;

            products = parentcat.GetProductCollection();

            if (products.Count > 0)
            {
                sb.Append("<ul>");
                foreach (Product p in products)
                {
                    sb.Append("<li style='list-style-type: none;'>");
                    sb.AppendFormat("<img src=\"/umbraco/images/umbraco/package.gif\" /> <a href=\"javascript:openProduct({1})\">{0}</a>", p.ProductName, p.ProductID);
                    sb.Append("</li>");
                }
                sb.Append("</ul>");
            }

            return sb.ToString();
        }

        protected void ShowProducts_CheckedChanged(object sender, EventArgs e)
        {
            BindData();
           
        }

    }
}